﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Drawing;

namespace DarcRogue
{
    public class Map
    {
        static Dictionary<TileType, Tile> tiledata = new Dictionary<TileType, Tile>();

        bool indoor;
        Player player;
        Hud hud;
        TileType[] tiles;
        int width;
        int height;

        public Player Player { get { return player; } }

        Map() { }
        public Map(string file, int x, int y)
        {
            BinaryReader br = FileManager.GetFile(file);
            width = br.ReadByte();
            height = br.ReadByte();
            tiles = new TileType[width * height];
            for (int i = 0; i < width * height; i++)
                tiles[i] = (TileType)br.ReadByte();
            player = new Player(this, x, y);
            br.Close();
        }

        public void Update()
        {
        }
        public void Draw()
        {
            Console.SetCursorPosition(0, 0);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                    if (GetTile(x, y) != null)
                        GetTile(x, y).Draw();
                    else
                        Console.Write(' ');
                Console.Write('\n');
            }
        }

        void CreateTiles(string s)
        {
            string[] split = s.Split('\n');
            tiles = new TileType[split.Length * split[0].Length];
            for (int y = 0; y < split.Length; y++)
                for (int x = 0; x < split[y].Length; x++)
                {
                    switch (split[y][x])
                    {
                        case '#':
                            EditTile(x, y, TileType.Wall);
                            break;
                        case ' ':
                            EditTile(x, y, TileType.Floor);
                            break;
                    }
                }
        }

        void EditTile(int x, int y, TileType t)
        {
            tiles[x + (y * width)] = t;
        }
        public Tile GetTile(int x, int y)
        {
            TileType t = tiles[x + (y * width)];
            return tiledata[t];
        }

        public Hud Hud { set { hud = value; player.Hud = value; } }
        public int Width { get { return width; } }
        public int Height { get { return height; } }

        public string GetMessage(int x, int y)
        {
            string s = "";
            if (GetTile(x, y) != null)
                s = GetTile(x, y).Description;
            if (x == player.Position.X && y == player.Position.Y)
                s = "You";
            return s;
        }
        public bool Solid(int x, int y)
        {
            bool ret = false;
            if (GetTile(x, y) != null)
                ret = GetTile(x, y).Solid;
            if (x == player.Position.X && y == player.Position.Y)
                ret = true;
            return ret;
        }

        public void Save(string file)
        {
            FileStream fs = new FileStream(file, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write((byte)width);
            bw.Write((byte)height);
            for (int i = 0; i < width * height; i++)
                bw.Write((byte)tiles[i]);
            bw.Close();
        }

        public static Map Default
        {
            get
            {
                Map m = new Map();
                m.height = 20;
                m.width = 50;
                m.CreateTiles(
                    "##################################################\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "#                                                #\n" +
                    "##################################################\n"
                    );
                m.player = new Player(m, 2, 2);
                return m;
            }
        }
        public static Map Random(Player p)
        {
            Map m = new Map();
            m.height = 20;
            m.width = 50;
            m.tiles = new TileType[20 * 50];
            m.player = p;
            return m;
        }

        /*public List<Point> FindPath(Point start, Point end)
        {
        }*/

        public static void Setup()
        {
            tiledata.Add(TileType.Wall, new Tile("Wall.dtl"));
            tiledata.Add(TileType.Floor, new Tile("Floor.dtl"));
        }
    }
}
